home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
cocktail
/
puma.lha
/
puma
/
src
/
mod.puma
< prev
next >
Wrap
Text File
|
1992-09-25
|
24KB
|
963 lines
/* Ich, Doktor Josef Grosch, Informatiker, 29.4.1991 */
TRAFO Mod
TREE Tree
PUBLIC DefMod ImplMod MacroMod
GLOBAL {
FROM Positions IMPORT tPosition;
FROM IO IMPORT StdOutput, WriteS, WriteNl;
FROM StringMem IMPORT WriteString;
FROM Idents IMPORT tIdent, NoIdent;
FROM Texts IMPORT WriteText;
FROM Sets IMPORT IsElement, IsNotEqual, Minimum, Maximum, IsEmpty;
FROM Semantics IMPORT IdentifyVar, LookupClass;
FROM Optimize IMPORT NeedsTempo, NeedsMatch, NeedsNoFinale, GetRule;
FROM Tree IMPORT NoTree, tTree, Options, f, SourceFile, WI, WN;
VAR
RoutineKind : (kProcedure, kFunction, kPredicate);
i, j : CARDINAL;
ListCount : INTEGER;
rule ,
TheClass ,
InFormals ,
OutFormals ,
ReturnFormals,
Decls : tTree;
TheName : tIdent;
TemposDone : BOOLEAN;
PROCEDURE WriteLine (Line: tPosition);
BEGIN
IF Line.Line # 0 THEN
!(* line ! WN (Line.Line); @ "@ WriteS (f, SourceFile); @" *)@
END;
END WriteLine;
PROCEDURE Match (t, Formals: tTree);
VAR TreeName : tIdent;
VAR Pattern : tTree;
BEGIN
IF (t^.Kind = Tree.NoPattern) OR (Formals^.Kind # Tree.Formal) THEN RETURN; END;
Pattern := t^.OnePattern.Pattern;
CASE Pattern^.Kind OF
| Tree.Decompose: WITH Pattern^.Decompose DO
TreeName := Object^.Class.TypeDesc^.NodeTypes.TreeName^.TreeName.Name;
IF (Formals^.Formal.TypeDesc^.Kind = Tree.UserType) OR
IsNotEqual (Object^.Class.TypeDesc^.NodeTypes.Types, Formals^.Formal.TypeDesc^.NodeTypes.Types) THEN
IF Object^.Class.Extensions^.Kind = Tree.NoClass THEN (* Low ? *)
! IF (! ImplMod (Path); !^.Kind # !
ELSE
! IF NOT ! WI (TreeName); !.IsType (! ImplMod (Path); !, !
END;
WI (TreeName); !.! WI (Object^.Class.Name); !) THEN EXIT; END;!
END;
Match (Patterns, Object^.Class.Formals);
END;
| Tree.VarDef: WITH Pattern^.VarDef DO
IF Object # NoTree THEN
WITH Object^.Formal DO
! IF NOT (equal! ImplMod (TypeDesc); ! (! ImplMod (Path);
!, ! ImplMod (Pattern^.VarDef.Path); !)) THEN EXIT; END;!
END;
END;
END;
| Tree.NilTest:
! IF ! ImplMod (Pattern^.NilTest.Path); ! # NIL THEN EXIT; END;!
| Tree.DontCare1:
| Tree.DontCare: RETURN;
| Tree.Value: WITH Pattern^.Value DO
AssignTempo (Expr);
! IF NOT (equal! ImplMod (Formals^.Formal.TypeDesc);
! (! ImplMod (Path); !, ! Expression (Expr); !)) THEN EXIT; END;!
MatchExpr (Expr);
END;
END;
Match (t^.OnePattern.Next, Formals^.Formal.Next);
END Match;
PROCEDURE MatchExprs (t: tTree);
BEGIN
IF t^.Kind = Tree.NoExpr THEN RETURN; END;
IF t^.OneExpr.Expr^.Kind = Tree.DontCare THEN RETURN; END;
MatchExpr (t^.OneExpr.Expr);
MatchExprs (t^.OneExpr.Next);
END MatchExprs;
PROCEDURE MatchExpr (t: tTree);
BEGIN
CASE t^.Kind OF
| Tree.Compose:
MatchExprs (t^.Compose.Exprs);
| Tree.VarUse :
| Tree.Nil :
| Tree.DontCare1 :
| Tree.TargetExpr :
| Tree.StringExpr :
| Tree.AttrDesc :
| Tree.Call : WITH t^.Call DO
MatchExpr (Expr);
MatchExprs (Exprs);
IF Object # NoTree THEN
Match (Patterns, Object^.Routine.OutForm);
END;
END;
| Tree.Binary : WITH t^.Binary DO
MatchExpr (Lop);
MatchExpr (Rop);
END;
| Tree.PreOperator, Tree.PostOperator :
MatchExpr (t^.PreOperator.Expr);
| Tree.Index :
MatchExpr (t^.Index.Expr);
MatchExprs (t^.Index.Exprs);
| Tree.Parents :
MatchExpr (t^.Parents.Expr);
END;
END MatchExpr;
PROCEDURE AssignTempos (t: tTree);
BEGIN
IF t^.Kind = Tree.NoExpr THEN RETURN; END;
IF t^.OneExpr.Expr^.Kind = Tree.DontCare THEN RETURN; END;
AssignTempo (t^.OneExpr.Expr);
AssignTempos (t^.OneExpr.Next);
END AssignTempos;
PROCEDURE AssignTempo (t: tTree);
VAR TreeName : tIdent;
BEGIN
CASE t^.Kind OF
| Tree.Compose: WITH t^.Compose DO
TreeName := Object^.Class.TypeDesc^.NodeTypes.TreeName^.TreeName.Name;
! yyALLOC (! WI (TreeName); !,Make! WI (TreeName); !,! WI (Tempo); !,! WI (Object^.Class.Name); !)!
IF (Exprs^.Kind = Tree.OneExpr) AND (Exprs^.OneExpr.Expr^.Kind # Tree.DontCare) THEN
! WITH ! WI (Tempo); !^.! WI (Object^.Class.Name); ! DO!
AssignFormals (Exprs, Object^.Class.Formals);
! END;!
END;
END;
| Tree.VarUse :
| Tree.Nil :
| Tree.DontCare1 :
| Tree.TargetExpr :
| Tree.StringExpr :
| Tree.AttrDesc :
| Tree.Call : WITH t^.Call DO
AssignTempo (Expr);
AssignTempos (Exprs);
END;
| Tree.Binary : WITH t^.Binary DO
AssignTempo (Lop);
AssignTempo (Rop);
END;
| Tree.PreOperator, Tree.PostOperator :
AssignTempo (t^.PreOperator.Expr);
| Tree.Index :
AssignTempo (t^.Index.Expr);
AssignTempos (t^.Index.Exprs);
| Tree.Parents :
AssignTempo (t^.Parents.Expr);
END;
END AssignTempo;
PROCEDURE AssignFormals (t, Formals: tTree);
BEGIN
IF (t^.Kind = Tree.NoExpr) OR (Formals^.Kind # Tree.Formal) THEN RETURN; END;
IF t^.OneExpr.Expr^.Kind = Tree.DontCare THEN
BeginFormals (Formals);
RETURN;
END;
AssignFormal (t^.OneExpr.Expr, Formals);
AssignFormals (t^.OneExpr.Next, Formals^.Formal.Next);
END AssignFormals;
PROCEDURE AssignFormal (t, Formals: tTree);
VAR TreeName : tIdent;
BEGIN
IF t^.Kind = Tree.Compose THEN
WITH t^.Compose DO
TreeName := Object^.Class.TypeDesc^.NodeTypes.TreeName^.TreeName.Name;
! yyALLOC (! WI (TreeName); !,Make! WI (TreeName); !,! WI (Formals^.Formal.Name); !,! WI (Object^.Class.Name); !)!
IF (Exprs^.Kind = Tree.OneExpr) AND (Exprs^.OneExpr.Expr^.Kind # Tree.DontCare) THEN
! WITH ! WI (Formals^.Formal.Name); !^.! WI (Object^.Class.Name); ! DO!
AssignFormals (Exprs, Object^.Class.Formals);
! END;!
END;
END;
ELSE
AssignTempo (t);
END;
CASE t^.Kind OF
| Tree.VarUse, Tree.Nil, Tree.Call, Tree.Binary, Tree.PreOperator,
Tree.PostOperator, Tree.Index, Tree.Parents, Tree.TargetExpr, Tree.StringExpr,
Tree.AttrDesc:
! ! WI (Formals^.Formal.Name); ! := ! Expression (t); !;!
| Tree.DontCare1:
! begin! ImplMod (Formals^.Formal.TypeDesc); ! (! WI (Formals^.Formal.Name); !)!
ELSE
END;
END AssignFormal;
PROCEDURE BeginFormals (Formals: tTree);
BEGIN
IF Formals^.Kind = Tree.Formal THEN
WITH Formals^.Formal DO
! begin! ImplMod (TypeDesc); ! (! WI (Name); !)!
BeginFormals (Next);
END;
END;
END BeginFormals;
PROCEDURE ConsPatterns (t: tTree; ListCount: INTEGER): INTEGER;
BEGIN
IF t^.Kind = Tree.NoPattern THEN RETURN ListCount; END;
WITH t^.OnePattern DO
IF Pattern^.Kind = Tree.DontCare THEN
RETURN ConsTempos (Pattern^.DontCare.Tempos, ListCount);
ELSE
IF ListCount > 0 THEN !, ! END;
WI (Pattern^.Pattern.Tempo);
RETURN ConsPatterns (Next, ListCount + 1);
END;
END;
END ConsPatterns;
PROCEDURE ConsTempos (t: tTree; ListCount: INTEGER): INTEGER;
BEGIN
IF t^.Kind = Tree.Formal THEN
IF ListCount > 0 THEN !, ! END;
WI (t^.Formal.Name);
RETURN ConsTempos (t^.Formal.Next, ListCount + 1);
ELSE
RETURN ListCount;
END;
END ConsTempos;
PROCEDURE Expressions (t: tTree; ListCount: INTEGER): INTEGER;
BEGIN
IF t^.Kind = Tree.NoExpr THEN RETURN ListCount; END;
WITH t^.OneExpr DO
IF Expr^.Kind = Tree.DontCare THEN
RETURN ConsTempos (Expr^.DontCare.Tempos, ListCount);
ELSE
IF ListCount > 0 THEN !, ! END;
Expression (Expr);
RETURN Expressions (Next, ListCount + 1);
END;
END;
END Expressions;
PROCEDURE Expression (t: tTree);
BEGIN
CASE t^.Kind OF
| Tree.Compose : WI (t^.Compose.Tempo);
| Tree.Nil : !NIL!
| Tree.VarUse : WITH t^.VarUse DO
IF Object # NoTree THEN
ImplMod (Object^.Formal.Path);
ELSE
WI (Name);
END;
END;
| Tree.DontCare1 : WI (t^.DontCare1.Tempo);
| Tree.Call : WITH t^.Call DO
Expression (Expr); ! (!
ListCount := Expressions (Exprs, 0);
IF Object # NoTree THEN
ListCount := ConsPatterns (Patterns, ListCount);
ELSE
ListCount := Expressions (Patterns, ListCount);
END;
!)!
END;
| Tree.Binary : WITH t^.Binary DO
Expression (Lop); ! ! WI (Operator); ! ! Expression (Rop);
END;
| Tree.PreOperator :
WI (t^.PreOperator.Operator); ! ! Expression (t^.PreOperator.Expr);
| Tree.PostOperator :
Expression (t^.PostOperator.Expr); ! ! WI (t^.PostOperator.Operator);
| Tree.Index :
Expression (t^.Index.Expr); ! [! ListCount := Expressions (t^.Index.Exprs, 0); !]!
| Tree.Parents : !(! Expression (t^.Parents.Expr); !)!
| Tree.TargetExpr : ImplMod (t^.TargetExpr.Expr);
| Tree.StringExpr : WriteString (f, t^.StringExpr.String);
| Tree.AttrDesc : WITH t^.AttrDesc DO
ImplMod (Object^.Formal.Path); !^.! WI (Type); !.! WI (Attribute);
END;
END;
END Expression;
PROCEDURE Tg2 (t, Formals: tTree);
BEGIN
IF (t^.Kind = Tree.NoPattern) OR (Formals^.Kind = Tree.NoFormal) THEN RETURN; END;
CASE t^.OnePattern.Pattern^.Kind OF
| Tree.Decompose:
! WITH ! WI (Formals^.Formal.Name); !^.! WI (t^.OnePattern.Pattern^.Decompose.Object^.Class.Name); ! DO!
| Tree.VarDef, Tree.NilTest, Tree.Value, Tree.DontCare1:
ELSE RETURN;
END;
Tg2 (t^.OnePattern.Next, Formals^.Formal.Next);
END Tg2;
}
PROCEDURE MacroMod (t: Tree)
Spec (..) :- {
MacroMod (TreeNames);
}; .
TreeName (..) :- {
!# define begint! WI (Name); !(a) a := NIL;!
!# define equalt! WI (Name); !(a, b) ! WI (Name); !.IsEqual! WI (Name); ! (a, b)!
MacroMod (Next);
}; .
PROCEDURE DefMod (t: Tree)
Spec (..) :- {
!DEFINITION MODULE ! WI (TrafoName); !;!
!!
!IMPORT SYSTEM, IO! DefMod (TreeNames); !;!
WriteLine (Codes^.Codes.ImportLine);
WriteText (f, Codes^.Codes.Import);
WriteLine (Codes^.Codes.ExportLine);
WriteText (f, Codes^.Codes.Export);
!!
!VAR yyf : IO.tFile;!
!VAR Exit : PROC;!
!!
DefMod (Public);
!!
!PROCEDURE Begin! WI (TrafoName); !;!
!PROCEDURE Close! WI (TrafoName); !;!
!!
!END ! WI (TrafoName); !.!
}; .
TreeName (..) :- {
!, ! WI (Name);
DefMod (Next);
}; .
Name (..) :- {
IF Object # NoTree THEN
ListCount := 0;
!PROCEDURE ! WI (Name); ! (!
DefMod (Object^.Routine.InForm);
DefMod (Object^.Routine.OutForm);
!)!
IF Object^.Kind = Tree.Predicate THEN
!: BOOLEAN!
ELSIF Object^.Kind = Tree.Function THEN
!: ! DefMod (Object^.Function.ReturnForm^.Formal.TypeDesc);
END;
!;!
END;
DefMod (Next);
}; .
Formal (..) :- {
IF ListCount > 0 THEN !; ! END;
IF Path^.Var.IsOutput THEN !VAR ! END;
WI (Name); !: !
DefMod (TypeDesc);
INC (ListCount);
DefMod (Next);
}; .
NodeTypes (..) :- {
WI (TreeName^.TreeName.Name); !.t! WI (TreeName^.TreeName.Name);
}; .
UserType (..) :- {
WI (Type);
}; .
PROCEDURE ImplMod (t: Tree)
Spec (..) :- {
!IMPLEMENTATION MODULE ! WI (TrafoName); !;!
IF NOT IsElement (ORD ('m'), Options) THEN
!# define yyInline!
END;
!# ifdef yyInline!
!# define yyALLOC(tree, make, ptr, kind) \!
! ptr := tree.yyPoolFreePtr; \!
! IF SYSTEM.ADDRESS (ptr) >= tree.yyPoolMaxPtr THEN ptr := tree.yyAlloc (); END; \!
! INC (tree.yyPoolFreePtr, tree.yyNodeSize [tree.kind]); \!
! ptr^.yyHead.yyMark := 0; \!
! ptr^.Kind := tree.kind;!
!# else!
!# define yyALLOC(tree, make, ptr, kind) ptr := tree.make (tree.kind);!
!# endif!
!!
!# define yyWrite(s) IO.WriteS (yyf, s)!
!# define yyWriteNl IO.WriteNl (yyf)!
!!
!IMPORT SYSTEM, System, IO! DefMod (TreeNames); !;!
WriteLine (Codes^.Codes.GlobalLine);
WriteText (f, Codes^.Codes.Global);
@# include "yy@ WI (TrafoName); @.w"@
!!
!PROCEDURE yyAbort (yyFunction: ARRAY OF CHAR);!
! BEGIN!
! IO.WriteS (IO.StdError, 'Error: module ! WI (TrafoName); !, routine ');!
! IO.WriteS (IO.StdError, yyFunction);!
! IO.WriteS (IO.StdError, ' failed');!
! IO.WriteNl (IO.StdError);!
! Exit;!
! END yyAbort;!
!!
!PROCEDURE yyIsEqual (yya, yyb: ARRAY OF SYSTEM.BYTE): BOOLEAN;!
! VAR yyi : INTEGER;!
! BEGIN!
! FOR yyi := 0 TO INTEGER (HIGH (yya)) DO!
! IF yya [yyi] # yyb [yyi] THEN RETURN FALSE; END;!
! END;!
! RETURN TRUE;!
! END yyIsEqual;!
!!
ImplMod (Routines);
!PROCEDURE Begin! WI (TrafoName); !;!
! BEGIN!
WriteLine (Codes^.Codes.BeginLine);
WriteText (f, Codes^.Codes.Begin);
! END Begin! WI (TrafoName); !;!
!!
!PROCEDURE Close! WI (TrafoName); !;!
! BEGIN!
WriteLine (Codes^.Codes.CloseLine);
WriteText (f, Codes^.Codes.Close);
! END Close! WI (TrafoName); !;!
!!
!PROCEDURE yyExit;!
! BEGIN!
! IO.CloseIO; System.Exit (1);!
! END yyExit;!
!!
!BEGIN!
! yyf := IO.StdOutput;!
! Exit := yyExit;!
! Begin! WI (TrafoName); !;!
!END ! WI (TrafoName); !.!
}; .
Procedure (..) :- {
ListCount := 0;
!PROCEDURE ! WI (Name); ! (!
DefMod (InForm);
DefMod (OutForm);
!);!
WriteLine (LocalLine);
WriteText (f, Local);
! VAR yyTempo: RECORD CASE : INTEGER OF!
RoutineKind := kProcedure;
InFormals := InForm;
OutFormals := OutForm;
Declare (Rules);
! END; END;!
! BEGIN!
IF IsElement (ORD ('n'), Options) THEN
Tg1 (InForm);
END;
IF IsElement (ORD ('b'), Options) THEN
ImplMod (Rules);
IF IsElement (ORD ('f'), Options) THEN
! yyAbort ('! WI (Name); !');!
END;
ELSE
TemposDone := FALSE;
CommonTestElim (Decisions);
IF IsElement (ORD ('f'), Options) AND NOT NeedsNoFinale (Decisions) THEN
! yyAbort ('! WI (Name); !');!
END;
END;
! END ! WI (Name); !;!
!!
ImplMod (Next);
}; .
Function (..) :- {
ListCount := 0;
!PROCEDURE ! WI (Name); ! (!
DefMod (InForm);
DefMod (OutForm);
!): ! DefMod (ReturnForm^.Formal.TypeDesc); !;!
WriteLine (LocalLine);
WriteText (f, Local);
! VAR yyTempo: RECORD CASE : INTEGER OF!
RoutineKind := kFunction;
InFormals := InForm;
OutFormals := OutForm;
ReturnFormals := ReturnForm;
Declare (Rules);
! END; END;!
! BEGIN!
IF IsElement (ORD ('b'), Options) THEN
ImplMod (Rules);
! yyAbort ('! WI (Name); !');!
ELSE
TemposDone := FALSE;
CommonTestElim (Decisions);
IF NOT NeedsNoFinale (Decisions) THEN
! yyAbort ('! WI (Name); !');!
END;
END;
! END ! WI (Name); !;!
!!
ImplMod (Next);
}; .
Predicate (..) :- {
ListCount := 0;
!PROCEDURE ! WI (Name); ! (!
DefMod (InForm);
DefMod (OutForm);
!): BOOLEAN;!
WriteLine (LocalLine);
WriteText (f, Local);
! VAR yyTempo: RECORD CASE : INTEGER OF!
RoutineKind := kPredicate;
InFormals := InForm;
OutFormals := OutForm;
Declare (Rules);
! END; END;!
! BEGIN!
IF IsElement (ORD ('n'), Options) THEN
Tg1 (InForm);
END;
IF IsElement (ORD ('b'), Options) THEN
ImplMod (Rules);
! RETURN FALSE;!
ELSE
TemposDone := FALSE;
CommonTestElim (Decisions);
IF NOT NeedsNoFinale (Decisions) THEN
! RETURN FALSE;!
END;
END;
! END ! WI (Name); !;!
!!
ImplMod (Next);
}; .
Rule (..) :- {
WriteLine (Line);
IF HasTempos THEN ! WITH yyTempo.yyR! WN (Index); ! DO! END;
! LOOP!
Decls := VarDecls;
Match (Patterns, InFormals);
IF IsElement (ORD ('w'), Options) AND (Statements^.Kind # Tree.NoStatement) THEN
Tg2 (Patterns, InFormals);
END;
ImplMod (Statements);
IF NOT HasRejectOrFail THEN
AssignFormals (Exprs, OutFormals);
MatchExprs (Exprs);
CASE RoutineKind OF
| kProcedure: ! RETURN;!
| kFunction :
AssignTempo (Expr);
IF HasPatterns AND (Expr^.Kind # Tree.Compose) AND (t^.Kind # Tree.DontCare1) THEN
! ! WI (Tempo); ! := ! Expression (Expr); !;!
MatchExpr (Expr);
! RETURN ! WI (Tempo); !;!
ELSE
MatchExpr (Expr);
! RETURN ! Expression (Expr); !;!
END;
| kPredicate: ! RETURN TRUE;!
END;
END;
IF IsElement (ORD ('w'), Options) AND (Statements^.Kind # Tree.NoStatement) THEN
Tg3 (Patterns);
END;
! END;!
IF HasTempos THEN ! END;!
END;
!!
ImplMod (Next);
}; .
ProcCall (..) :- {
WriteLine (Pos);
AssignTempo (Call);
! ! Expression (Call); !;!
MatchExpr (Call);
ImplMod (Next);
}; .
Condition (..) :- {
WriteLine (Pos);
AssignTempo (Expr);
! IF NOT (! Expression (Expr); !) THEN EXIT; END;!
MatchExpr (Expr);
ImplMod (Next);
}; .
Assignment (..) :- {
WriteLine (Pos);
AssignTempo (Adr);
AssignTempo (Expr);
IF Object # NoTree THEN
! ! ImplMod (Object^.Formal.Path);
ELSE
! ! Expression (Adr);
END;
! := ! Expression (Expr); !;!
MatchExpr (Adr);
MatchExpr (Expr);
ImplMod (Next);
}; .
Reject (..) :- {
WriteLine (Pos);
! EXIT;!
}; .
Fail (..) :- {
WriteLine (Pos);
! RETURN! IF RoutineKind = kPredicate THEN ! FALSE! END; !;!
}; .
TargetStmt (..) :- {
WriteLine (Pos);
! ! ImplMod (Stmt); !;!
ImplMod (Next);
}; .
Nl (..) :- {
WriteLine (Pos);
! yyWriteNl;!
ImplMod (Next);
}; .
WriteStr (..) :- {
WriteLine (Pos);
! yyWrite (! WriteString (f, String); !);!
ImplMod (Next);
}; .
Ident (..) :- Var: tTree; {
Var := IdentifyVar (Decls, Attribute);
IF Var # NoTree THEN ImplMod (Var^.Formal.Path); ELSE WI (Attribute); END;
ImplMod (Next);
}; .
Any (..) :- {
WriteString (f, Code);
ImplMod (Next);
}; .
Anys (..) :- {
ImplMod (Layouts);
ImplMod (Next);
}; .
LayoutAny (..) :- {
WriteString (f, Code);
ImplMod (Next);
}; .
Designator (..) :- {
ImplMod (Object^.Formal.Path); !^.! WI (Type); !.! WI (Attribute);
ImplMod (Next);
}; .
Field (..) :- {
ImplMod (Next);
!.! WI (Name);
}; .
ConsType (..) :- {
ImplMod (Next);
!^.! WI (Name);
}; .
Var (..) :- {
WI (Name);
}; .
NodeTypes (..) :- {
!t! WI (TreeName^.TreeName.Name);
}; .
UserType (..) :- {
WI (Type);
}; .
PROCEDURE Declare (t: Tree)
Formal (..) :- {
! ! WI (Name); !: ! DefMod (TypeDesc); !;!
Declare (Next);
}; .
Param (..) :- Var: tTree; {
Var := IdentifyVar (Decls, Name);
! ! WI (Name); !: ! DefMod (Var^.Formal.TypeDesc); !;!
Declare (Next);
}; .
Rule (..) :- {
IF HasTempos THEN
! | ! WN (Index); !: yyR! WN (Index); !: RECORD!
Decls := VarDecls;
Declare (Patterns);
Declare (Exprs);
IF RoutineKind = kFunction THEN
Declare (Expr);
IF HasPatterns AND (Expr^.Kind # Tree.Compose) AND (t^.Kind # Tree.DontCare1) THEN
! ! WI (Tempo); !: ! DefMod (ReturnFormals^.Formal.TypeDesc); !;!
END;
END;
Declare (Statements);
! END;!
END;
Declare (Next);
}; .
ProcCall (..) :- {
Declare (Call);
Declare (Next);
}; .
Condition (..) :- {
Declare (Expr);
Declare (Next);
}; .
Assignment (..) :- {
Declare (Adr);
Declare (Expr);
Declare (Next);
}; .
TargetStmt (..) :- {
Declare (Parameters);
Declare (Next);
}; .
Statement (..) :- {
Declare (Next);
}; .
OnePattern (..) :- {
IF (Pattern^.Pattern.Tempo # NoIdent) AND (Pattern^.Kind # Tree.DontCare1) THEN
! ! WI (Pattern^.Pattern.Tempo); !: ! DefMod (Pattern^.Pattern.TypeDesc); !;!
END;
Declare (Pattern);
Declare (Next);
}; .
OneExpr (..) :- {
Declare (Expr);
Declare (Next);
}; .
Decompose (..) :- {
Declare (Patterns);
}; .
DontCare (..) :- {
Declare (Tempos);
}; .
DontCare1 (..) :- {
IF Tempo # NoIdent THEN
! ! WI (Tempo); !: ! DefMod (TypeDesc); !;!
END;
}; .
Value (..) :- {
Declare (Expr);
}; .
Compose (..) :- {
IF Tempo # NoIdent THEN
! ! WI (Tempo); !: ! DefMod (TypeDesc); !;!
END;
Declare (Exprs);
}; .
Call (..) :- {
Declare (Expr);
Declare (Exprs);
Declare (Patterns);
}; .
Binary (..) :- {
Declare (Lop);
Declare (Rop);
}; .
PreOperator (..) ;
PostOperator (..) ;
Parents (..) :- {
Declare (Expr);
}; .
Index (..) :- {
Declare (Expr);
Declare (Exprs);
}; .
PROCEDURE Tg1 (t: Tree)
Formal (..) :- {
TheName := Name;
Tg1 (TypeDesc);
Tg1 (Next);
}; .
NodeTypes (..) :- {
! IF ! WI (TheName); ! = ! WI (TreeName^.TreeName.Name); !.No!
WI (TreeName^.TreeName.Name); ! THEN RETURN! IF RoutineKind = kPredicate THEN ! FALSE! END; !; END;!
}; .
PROCEDURE Tg3 (t: Tree)
OnePattern (..) :- {
Tg3 (Pattern);
Tg3 (Next);
}; .
Decompose (..) :- {
! END;!
}; .
PROCEDURE CommonTestElim2 (OneTest: OneTest, Decisions)
TestValue (..), Then :-
(NeedsMatch (Expr));
CommonTestElim (OneTest);
! LOOP!
MatchExpr (OneTest^.TestValue.Expr);
CommonTestElim (Then);
! EXIT; END;!
.
_, Then :-
CommonTestElim (OneTest);
CommonTestElim (Then);
.
PROCEDURE CommonTestElim (t: Tree)
Decision (..) :- {
IF Cases = 0 THEN
IF NOT TemposDone AND (OneTest^.Kind = Tree.TestValue) AND NeedsTempo (Then, rule) THEN
! WITH yyTempo.yyR! WN (rule^.Rule.Index); ! DO!
TemposDone := TRUE;
CommonTestElim2 (OneTest, Then);
! END;!
! END;!
ELSE
GetRule (Then, rule);
Decls := rule^.Rule.VarDecls;
CommonTestElim2 (OneTest, Then);
! END;!
END;
TemposDone := FALSE;
CommonTestElim (Else);
ELSE
i := Cases; Case (t);
END;
}; .
Decided (..) :- {
CommonTestElim (Rule);
IF Rule^.Rule.HasExit THEN
TemposDone := FALSE;
CommonTestElim (Else);
END;
}; .
TestKind (..) :- {
! IF (! ImplMod (Path); !^.Kind = !
WI (TypeDesc^.NodeTypes.TreeName^.TreeName.Name); !.! WI (Name); !) THEN!
}; .
TestIsType (..) :- {
! IF ! WI (TypeDesc^.NodeTypes.TreeName^.TreeName.Name); !.IsType (! ImplMod (Path); !, !
WI (TypeDesc^.NodeTypes.TreeName^.TreeName.Name); !.! WI (Name); !) THEN!
}; .
TestNil (..) :- {
! IF ! ImplMod (Path); ! = NIL THEN!
}; .
TestNonlin (..) :- {
! IF (equal! ImplMod (TypeDesc); ! (! ImplMod (Path);
!, ! ImplMod (Path2); !)) THEN!
}; .
TestValue (..) :- {
AssignTempo (Expr);
! IF (equal! ImplMod (TypeDesc); ! (! ImplMod (Path);
!, ! Expression (Expr); !)) THEN!
}; .
Rule (..) :- {
WriteLine (Line);
IF HasTempos AND NOT TemposDone THEN ! WITH yyTempo.yyR! WN (Index); ! DO!
END;
IF HasExit THEN ! LOOP!
END;
Decls := VarDecls;
IF IsElement (ORD ('w'), Options) AND (Statements^.Kind # Tree.NoStatement) THEN
Tg2 (Patterns, InFormals);
END;
ImplMod (Statements);
IF NOT HasRejectOrFail THEN
AssignFormals (Exprs, OutFormals);
MatchExprs (Exprs);
CASE RoutineKind OF
| kProcedure: ! RETURN;!
| kFunction :
AssignTempo (Expr);
IF HasPatterns AND (Expr^.Kind # Tree.Compose) AND (t^.Kind # Tree.DontCare1) THEN
! ! WI (Tempo); ! := ! Expression (Expr); !;!
MatchExpr (Expr);
! RETURN ! WI (Tempo); !;!
ELSE
MatchExpr (Expr);
! RETURN ! Expression (Expr); !;!
END;
| kPredicate: ! RETURN TRUE;!
END;
END;
IF IsElement (ORD ('w'), Options) AND (Statements^.Kind # Tree.NoStatement) THEN
Tg3 (Patterns);
END;
IF HasExit THEN ! END;!
END;
IF HasTempos AND NOT TemposDone THEN ! END;!
END;
!!
}; .
PROCEDURE Case (t: Tree) /* reads GLOBAL i */
Decision (..) :- n: CARDINAL; {
!!
! CASE ! ImplMod (OneTest^.OneTest.Path); !^.Kind OF!
n := i;
WHILE n > 0 DO
IF NOT IsEmpty (t^.Decision.OneTest^.TestIsType.TypeDesc^.NodeTypes.Types) THEN
Case (t^.Decision.OneTest);
CommonTestElim (t^.Decision.Then);
END;
t := t^.Decision.Else;
DEC (n);
END;
! ELSE END;!
!!
CommonTestElim (t);
}; .
TestKind (..) :- {
! | ! WI (TypeDesc^.NodeTypes.TreeName^.TreeName.Name); !.! WI (Name); !:!
}; .
TestIsType (..) :- {
WITH TypeDesc^.NodeTypes DO
TheClass := LookupClass (TreeName^.TreeName.Classes, Minimum (Types));
! | ! WI (TreeName^.TreeName.Name); !.! WI (TheClass^.Class.Name);
Case (TypeDesc); !:!
END;
}; .
NodeTypes (..) :- {
FOR j := Minimum (Types) + 1 TO Maximum (Types) DO
IF IsElement (j, Types) THEN
TheClass := LookupClass (TreeName^.TreeName.Classes, j); !!
! , ! WI (TreeName^.TreeName.Name); !.! WI (TheClass^.Class.Name);
END;
END;
}; .